'''
titlescreen
Author: Alex Foran
'''

import pygame,gameroot,pane,loaders,math,sys,dialogs,random,dialogelements,gamelogic,creditscreen

'''
The beginning screen for the game. Serves as the launch point for everything good.
'''
class TitleScreen (pane.Pane):
	'''
	Constructor.
	
	Params:
	root - A reference to the current gameroot.
	'''
	def __init__(self,root):
		pane.Pane.__init__(self,root)
		#load images
		self.logo=loaders.load_image_alpha(gameroot.respath+"title/bubblelogo.png")
		self.arrow=loaders.load_image_alpha(gameroot.respath+"dialogs/arrow-alpha.png")
		#calculate display area; we always want to fit into 640x480
		self.xbox=(self.root.prefs["resolution"][0]-640)/2
		self.ybox=(self.root.prefs["resolution"][1]-480)/2
		#load elements
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+200,self.ybox+100,"Solo Game",self.root.font24,self.arrow,pygame.color.Color("white"),self.option_solo))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+200,self.ybox+150,"Network Game",self.root.font24,self.arrow,pygame.color.Color("white"),self.option_network))
		#self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+200,self.ybox+200,"Online Game",self.root.font24,self.arrow,pygame.color.Color("white"),self.option_online))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+200,self.ybox+200,"Options",self.root.font24,self.arrow,pygame.color.Color("white"),self.option_options))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+200,self.ybox+250,"Credits",self.root.font24,self.arrow,pygame.color.Color("white"),self.option_credits))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+200,self.ybox+400,"Quit",self.root.font24,self.arrow,pygame.color.Color("white"),self.option_exit))
		self.givefocus(self.elements[0])
		#load stars
		self.stars=range(200)
		for i in range(len(self.stars)):
			self.stars[i]=TitleStar()
		self.star=loaders.load_image_alpha(gameroot.respath+"title/star.png")
		#load bgm
		self.bgm=loaders.load_sound(gameroot.respath+"music/19-2000.ogg")
		self.root.audiosys.set_bgm(self.bgm)
	
	'''
	Launches a new solo game.
	'''
	def option_solo(self):
		self.root.pane=gamelogic.SoloGame(self.root)
	
	'''
	Launches a new network game.
	'''
	def option_network(self):
		self.root.pane.dialog=dialogs.LoginDialog(self.root,gamelogic.NetworkGame)
	
	'''
	Launches a new online game.
	'''
	def option_online(self):
		self.done = True
	
	'''
	Displays an options dialog.
	'''
	def option_options(self):
		self.dialog=dialogs.OptionsDialog(self.root)
	
	'''
	Displays credits.
	'''
	def option_credits(self):
		self.root.pane=creditscreen.CreditScreen(self.root)
	
	'''
	Exits the game.
	'''
	def option_exit(self):
		self.done = True
	
	'''
	Update.
	'''
	def update(self):
		pane.Pane.update(self)
		if self.dialog==None and self.root.monitor.getjustpressed("ESCAPE"):
			self.done=True
		#update stars
		for i in range(len(self.stars)):
			self.stars[i].step()
			if self.stars[i].dead:
				self.stars[i]=TitleStar(0)
	
	'''
	Paint.
	'''
	def repaint(self):
		surface=pygame.display.get_surface()
		#draw background
		surface.fill(pygame.color.Color("black"))
		#draw the stars in a psuedo-3D manner... makes for a neat effect
		xmid=self.root.prefs["resolution"][0]/2.0
		ymid=self.root.prefs["resolution"][1]/2.0
		for i in range(len(self.stars)):
			distance=self.stars[i].distance/1000.0*math.sqrt(xmid*xmid+ymid*ymid)
			xdist=math.cos(self.stars[i].angle)*distance;
			ydist=math.sin(self.stars[i].angle)*distance;
			scale=int(math.ceil(self.stars[i].distance/80.0))
			if scale<1:
				scale=1
			surface.blit(pygame.transform.scale(self.star,(scale,scale)),(xmid+xdist-scale/2,ymid+ydist-scale/2))
		#draw the logo
		surface.blit(self.logo,(self.xbox+30,self.ybox))
		pane.Pane.repaint(self)

'''
The little stars that get displayed on the title screen. When a star reaches a certain point it is deemed "dead" and is recycled.
'''
class TitleStar:
	'''
	Constructor.
	'''
	def __init__(self,maxdist=0):
		self.angle=random.randint(0,360)
		self.distance=0
		self.velocity=random.random()*25
		self.dead=False
	
	'''
	A mini version of the update() method everything else has.
	'''
	def step(self):
		self.distance+=self.velocity
		# if the distance is >1000 away, the star should be recycled
		self.dead=self.distance>1000